﻿using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class D_Account : IAccount
    {
        public void Add(Account item)
        {
            CakeEntities db = new CakeEntities();
            db.Account.Add(item);
            db.SaveChanges();
        }

        public void Delete(int id)
        {
            CakeEntities db = new CakeEntities();
            db.Account.Remove(db.Account.Find(id));
            db.SaveChanges();
        }

        public Account FindById(int id)
        {
            return new CakeEntities().Account.Find(id);
        }

        public List<Account> GetAll()
        {
            return new CakeEntities().Account.ToList();
        }

        public void Updata(int id, Account item)
        {
            CakeEntities db = new CakeEntities();
            var data=db.Account.Find(id);
            if (data != null)
            {
                data.Account1 = item.Account1;
                data.Password = item.Password;
                data.IsEnable = item.IsEnable;
                data.LastTiem=item.LastTiem;
                db.SaveChanges();
            }
        }

        //public bool HasUsername(string username)
        //{
            
        //    return new CakeEntities().Account.Where(x=>x.Account1 == username).Any();
        //}

        public int Login(string username, string password)
        {
            CakeEntities db = new CakeEntities();
            var data = db.Account.Where(x => x.Account1 == username && x.Password == password && x.IsEnable.Value).FirstOrDefault();
            if (data != null)
            {
                return data.Id;
            }
            else
            {
                return 0;
            }
        }

        public bool HasUserName(string username)
        {
            throw new NotImplementedException();
        }
    }
}
